#include "stdafx.h"
#include "FrameListener.h"

bool SceneListener::frameStarted(const Ogre::FrameEvent &evt)
{
  	Vector3 node1Pos = node1->getPosition();
  	Ray nodeRay(Vector3(node1Pos.x, 5000.0f, node1Pos.z), Vector3::NEGATIVE_UNIT_Y);
  	mRaySceneQuery->setRay(nodeRay);
  	RaySceneQueryResult &result = mRaySceneQuery->execute();
  	RaySceneQueryResult::iterator itr = result.begin();
  	if (itr != result.end() && itr->worldFragment)
  	{
  		Real terrainHeight = itr->worldFragment->singleIntersection.y;
  		//if ((terrainHeight + 10.0f) > node1Pos.y)
  		node1->setPosition( node1Pos.x, terrainHeight +  4.0f, node1Pos.z );
  	}
	ani_state->addTime(evt.timeSinceLastFrame);
    updateCollision(evt.timeSinceLastFrame);
/*	mCollideContext->collide(evt.timeSinceLastFrame);*/
   return true;
}


void SceneListener::updateCollision(Real timeDelta)
{
	static float lastVelocity = 0.0f;
	static float acc = -9.8f;

	CollisionContext *_pCollContext = 
		OgreOpcode::CollisionManager::getSingleton().getContext("testContext");
	_pCollContext->visualize(show_shape, false, false, false, true, false);
	_pCollContext->collide(timeDelta);

	SceneManager *_pSceneMgr = Ogre::Root::getSingleton().getSceneManager("ExampleSMInstance");
	Node *pNode = _pSceneMgr->getRootSceneNode()->getChild("people");

	if (pNode != NULL) {
	if ( _pCollContext->getAttachedObject("people")->hasCollisions() )
        moveflag=false;
	else
		moveflag=true;
	}
}